home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / gdata / tlslite / HandshakeSettings.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  6.3 KB  |  179 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Class for setting handshake parameters.'''
  5. from constants import CertificateType
  6. from utils import cryptomath
  7. from utils import cipherfactory
  8.  
  9. class HandshakeSettings:
  10.     """This class encapsulates various parameters that can be used with
  11.     a TLS handshake.
  12.     @sort: minKeySize, maxKeySize, cipherNames, certificateTypes,
  13.     minVersion, maxVersion
  14.  
  15.     @type minKeySize: int
  16.     @ivar minKeySize: The minimum bit length for asymmetric keys.
  17.  
  18.     If the other party tries to use SRP, RSA, or Diffie-Hellman
  19.     parameters smaller than this length, an alert will be
  20.     signalled.  The default is 1023.
  21.  
  22.     @type maxKeySize: int
  23.     @ivar maxKeySize: The maximum bit length for asymmetric keys.
  24.  
  25.     If the other party tries to use SRP, RSA, or Diffie-Hellman
  26.     parameters larger than this length, an alert will be signalled.
  27.     The default is 8193.
  28.  
  29.     @type cipherNames: list
  30.     @ivar cipherNames: The allowed ciphers, in order of preference.
  31.  
  32.     The allowed values in this list are 'aes256', 'aes128', '3des', and
  33.     'rc4'.  If these settings are used with a client handshake, they
  34.     determine the order of the ciphersuites offered in the ClientHello
  35.     message.
  36.  
  37.     If these settings are used with a server handshake, the server will
  38.     choose whichever ciphersuite matches the earliest entry in this
  39.     list.
  40.  
  41.     NOTE:  If '3des' is used in this list, but TLS Lite can't find an
  42.     add-on library that supports 3DES, then '3des' will be silently
  43.     removed.
  44.  
  45.     The default value is ['aes256', 'aes128', '3des', 'rc4'].
  46.  
  47.     @type certificateTypes: list
  48.     @ivar certificateTypes: The allowed certificate types, in order of
  49.     preference.
  50.  
  51.     The allowed values in this list are 'x509' and 'cryptoID'.  This
  52.     list is only used with a client handshake.  The client will
  53.     advertise to the server which certificate types are supported, and
  54.     will check that the server uses one of the appropriate types.
  55.  
  56.     NOTE:  If 'cryptoID' is used in this list, but cryptoIDlib is not
  57.     installed, then 'cryptoID' will be silently removed.
  58.  
  59.     @type minVersion: tuple
  60.     @ivar minVersion: The minimum allowed SSL/TLS version.
  61.  
  62.     This variable can be set to (3,0) for SSL 3.0, (3,1) for
  63.     TLS 1.0, or (3,2) for TLS 1.1.  If the other party wishes to
  64.     use a lower version, a protocol_version alert will be signalled.
  65.     The default is (3,0).
  66.  
  67.     @type maxVersion: tuple
  68.     @ivar maxVersion: The maximum allowed SSL/TLS version.
  69.  
  70.     This variable can be set to (3,0) for SSL 3.0, (3,1) for
  71.     TLS 1.0, or (3,2) for TLS 1.1.  If the other party wishes to
  72.     use a higher version, a protocol_version alert will be signalled.
  73.     The default is (3,2).  (WARNING: Some servers may (improperly)
  74.     reject clients which offer support for TLS 1.1.  In this case,
  75.     try lowering maxVersion to (3,1)).
  76.     """
  77.     
  78.     def __init__(self):
  79.         self.minKeySize = 1023
  80.         self.maxKeySize = 8193
  81.         self.cipherNames = [
  82.             'aes256',
  83.             'aes128',
  84.             '3des',
  85.             'rc4']
  86.         self.cipherImplementations = [
  87.             'cryptlib',
  88.             'openssl',
  89.             'pycrypto',
  90.             'python']
  91.         self.certificateTypes = [
  92.             'x509',
  93.             'cryptoID']
  94.         self.minVersion = (3, 0)
  95.         self.maxVersion = (3, 2)
  96.  
  97.     
  98.     def _filter(self):
  99.         other = HandshakeSettings()
  100.         other.minKeySize = self.minKeySize
  101.         other.maxKeySize = self.maxKeySize
  102.         other.cipherNames = self.cipherNames
  103.         other.cipherImplementations = self.cipherImplementations
  104.         other.certificateTypes = self.certificateTypes
  105.         other.minVersion = self.minVersion
  106.         other.maxVersion = self.maxVersion
  107.         if len(other.cipherNames) == 0:
  108.             raise ValueError('No supported ciphers')
  109.         len(other.cipherNames) == 0
  110.         
  111.         try:
  112.             import cryptoIDlib
  113.         except ImportError:
  114.             None if not cipherfactory.tripleDESPresent else []
  115.             None if not cipherfactory.tripleDESPresent else []
  116.             other.certificateTypes = _[2]
  117.         except:
  118.             []
  119.  
  120.         if len(other.certificateTypes) == 0:
  121.             raise ValueError('No supported certificate types')
  122.         len(other.certificateTypes) == 0
  123.         if len(other.cipherImplementations) == 0:
  124.             raise ValueError('No supported cipher implementations')
  125.         len(other.cipherImplementations) == 0
  126.         if other.minKeySize < 512:
  127.             raise ValueError('minKeySize too small')
  128.         other.minKeySize < 512
  129.         if other.minKeySize > 16384:
  130.             raise ValueError('minKeySize too large')
  131.         other.minKeySize > 16384
  132.         if other.maxKeySize < 512:
  133.             raise ValueError('maxKeySize too small')
  134.         other.maxKeySize < 512
  135.         if other.maxKeySize > 16384:
  136.             raise ValueError('maxKeySize too large')
  137.         other.maxKeySize > 16384
  138.         for s in other.cipherNames:
  139.             if s not in ('aes256', 'aes128', 'rc4', '3des'):
  140.                 raise ValueError("Unknown cipher name: '%s'" % s)
  141.             s not in ('aes256', 'aes128', 'rc4', '3des')
  142.         
  143.         for s in other.cipherImplementations:
  144.             if s not in ('cryptlib', 'openssl', 'python', 'pycrypto'):
  145.                 raise ValueError("Unknown cipher implementation: '%s'" % s)
  146.             s not in ('cryptlib', 'openssl', 'python', 'pycrypto')
  147.         
  148.         for s in other.certificateTypes:
  149.             if s not in ('x509', 'cryptoID'):
  150.                 raise ValueError("Unknown certificate type: '%s'" % s)
  151.             s not in ('x509', 'cryptoID')
  152.         
  153.         if other.minVersion > other.maxVersion:
  154.             raise ValueError('Versions set incorrectly')
  155.         other.minVersion > other.maxVersion
  156.         if other.minVersion not in ((3, 0), (3, 1), (3, 2)):
  157.             raise ValueError('minVersion set incorrectly')
  158.         other.minVersion not in ((3, 0), (3, 1), (3, 2))
  159.         if other.maxVersion not in ((3, 0), (3, 1), (3, 2)):
  160.             raise ValueError('maxVersion set incorrectly')
  161.         other.maxVersion not in ((3, 0), (3, 1), (3, 2))
  162.         return other
  163.  
  164.     
  165.     def _getCertificateTypes(self):
  166.         l = []
  167.         for ct in self.certificateTypes:
  168.             if ct == 'x509':
  169.                 l.append(CertificateType.x509)
  170.                 continue
  171.             if ct == 'cryptoID':
  172.                 l.append(CertificateType.cryptoID)
  173.                 continue
  174.             raise AssertionError()
  175.         
  176.         return l
  177.  
  178.  
  179.